package com.wb.utils

import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.util.*

/**
 * 时间工具
 *
 * @author 祁雪
 */
class DateUtil {

    companion object {

        val SIMPLEDATEFORMAT: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        /**
         * 获取今年年份
         */
        fun getYear(): Int {

            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc.toZonedDateTime().year
        }

        fun getDay(): Int {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc.toZonedDateTime().dayOfMonth
        }

        /**
         * 获取一个时间
         */
        fun getDate(): String {
            return SIMPLEDATEFORMAT.format(Date())
        }

        /**
         * 根据时间格式获取一个时间
         *
         * @param format 时间格式 yyyy-MM-dd等
         */
        fun getDate(format: String): String {
            return SimpleDateFormat(format).format(Date())
        }

        fun getDate(
            format: String,
            date: Date
        ): String {
            return SimpleDateFormat(format).format(date)
        }


        /**
         * 获取本月是哪一月
         */
        fun getNowMonth(): Int {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc.toZonedDateTime().monthValue
        }

        fun getHours(): Int {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc.toZonedDateTime().hour
        }

        fun getMinutes(): Int {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc.toZonedDateTime().minute
        }

        fun getSeconds(): Int {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc.toZonedDateTime().second
        }

        /**
         * 两个日期相减得到的天数
         */
        fun getDateDiffDays(beginDate: Date?, endDate: Date?): Int {
            require(!(beginDate == null || endDate == null)) { "getDiffDays param is null!" }
            val diff = ((endDate.time - beginDate.time)
                    / (1000 * 60 * 60 * 24))
            return diff.toInt()
        }

        /**
         * 两个日期相减得到的毫秒数
         */
        fun getDateDiff(beginDate: Date, endDate: Date): Long {
            val date1ms = beginDate.time
            val date2ms = endDate.time
            return date2ms - date1ms
        }

        /**
         * 两个日期相减得到的秒数
         */
        fun getDateDiffSec(beginDate: Date, endDate: Date): Int {
            val date1ms = beginDate.time
            val date2ms = endDate.time
            val haomiao = date2ms - date1ms
            return (haomiao / 1000).toInt()
        }

        /**
         * 两个日期相减得到的分钟数(保留一位)
         */
        fun getDateMin(beginDate: Date, endDate: Date): Int {
            val date1ms = beginDate.time
            val date2ms = endDate.time
            val haomiao = date2ms - date1ms
            return (haomiao / (1000 * 60)).toInt()
        }

        /**
         * 获取两个日期中的最大日期
         */
        fun max(beginDate: Date?, endDate: Date?): Date? {
            if (beginDate == null) {
                return endDate
            }
            if (endDate == null) {
                return beginDate
            }
            return if (beginDate.after(endDate)) {
                beginDate
            } else endDate
        }

        /**
         * 获取两个日期中的最小日期
         */
        fun min(beginDate: Date?, endDate: Date?): Date? {
            if (beginDate == null) {
                return endDate
            }
            if (endDate == null) {
                return beginDate
            }
            return if (beginDate.after(endDate)) {
                endDate
            } else beginDate
        }

        /**
         * 返回某月该季度的第一个月
         */
        fun getFirstSeasonDate(date: Date?): Date? {
            val SEASON = intArrayOf(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
            val cal = Calendar.getInstance()
            cal.time = date
            val sean = SEASON[cal[Calendar.MONTH]]
            cal[Calendar.MONTH] = sean * 3 - 3
            return cal.time
        }

        fun getDate(time: Long): Date {
            return SIMPLEDATEFORMAT.parse(SIMPLEDATEFORMAT.format(time))
        }

    }

}

fun main() {

    println(LocalDateTime.now().hour)
}